package popcorn.ticket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.entity.MovieHalls;
import popcorn.ticket.serviceimpl.MovieHallsServiceImpl;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

/**
 * 影厅控制器
 * 
 * @author lenovo
 *
 */
@RestController
public class MovieHallsController {
	@Autowired
	MovieHallsServiceImpl mhsi;

	/**
	 * 分页+模糊查询
	 * 
	 * @param page
	 * @param limit
	 * @param keywords
	 * @return
	 */
	@GetMapping(value = "selectMovieHall", produces = "application/json;charset=utf-8")
	public PageData<MovieHalls> selectMovieHall(@RequestParam(value = "page",defaultValue = "1")int page, @RequestParam(value = "limit",defaultValue = "10")int limit, @RequestParam(value = "keywords",defaultValue = "")String keywords) {
		return mhsi.selectMovieHallsByPage(page, limit, keywords);
	}

	/**
	 * 添加
	 * 
	 * @param mh
	 * @return
	 */
	@PostMapping(value = "addMovieHall", produces = "application/json;charset=utf-8",consumes = "application/json")
	public AjaxResponse addMovieHall(@RequestBody  MovieHalls mh) {

		int n = mhsi.addMovieHalls(mh);
		int code = 1;
		String msg = "添加成功！";
		if (n == 0) {
			code = 0;
			msg = "添加失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 修改
	 * 
	 * @param mh
	 * @return
	 */
	@PostMapping(value = "updMovieHall", produces = "application/json;charset=utf-8",consumes = "application/json")
	public AjaxResponse updMovieHall(@RequestBody MovieHalls mh) {

		int n = mhsi.updMovieHalls(mh);
		int code = 1;
		String msg = "修改成功！";
		if (n == 0) {
			code = 0;
			msg = "修改失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 删除
	 * 
	 * @param mh
	 * @return
	 */
	@GetMapping(value = "delMovieHall/{hallid}", produces = "application/json;charset=utf-8")
	public AjaxResponse delMovieHall(@PathVariable("hallid")int hallid) {
		int n = mhsi.delMovieHalls(hallid);
		int code = 1;
		String msg = "删除成功！";
		if (n == 0) {
			code = 0;
			msg = "删除失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
	/**
	 * 批量删除
	 */
	@ResponseBody
	@PostMapping(value = "delAllMovieHall", produces = "application/json;charset=utf-8")
	public AjaxResponse delBlog(String array) {
		int n=mhsi.delAllMovieHall(array);
		String msg="成功删除了"+n+"条记录！";
		int code=1;
		AjaxResponse ar=new AjaxResponse(msg, code);
		return ar;
	}
	
}
